Skip to content

feat(tree): add the tree GC tick - #439

Merged
baer merged 15 commits into
tree/fold-tick-budgetfrom
tree/gc-tick
Nov 25, 2026
Merged

baer merged 15 commits into
tree/fold-tick-budgetfrom
tree/gc-tick

Conversation

@baer

@baer baer commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What & why

Adds the tree garbage-collection tick: a state codec, a probe-paced drain of superseded nodes and fold records, and a strict fold-record decoder. Like the layer below it, nothing outside packages/server/src/tree/ consumes it yet.

Key points

  • Each tick checks every superseded node's identity before deleting it, and paces itself to what the request ledger can fit.
  • Drain rate is fixed per probe tick. If tree folds later fire on the flat layout's count trigger, fold frequency can outgrow it, which delays reclamation but is not unsafe; size the trigger against it when the tree is wired into the write tick.
  • The tree GC state lives under its own prefix, apart from the flat layout's gc/pending.json.

Verification

| pnpm verify:agent | clean |
| pnpm build:agent | clean |
| pnpm test:agent | 6530 passed, 116 skipped |
| pnpm bundle-sizes | 14/14 |

Notes for reviewers

Stacked on the fold tick budget PR.

baer added 14 commits November 25, 2026 11:55
GC reads fold records to nominate deletions, so it needs a decoder
that accepts only what a publication sealed at that key: the body
ceiling, the key's hash against the exact bytes, fatal UTF-8, JSON,
every rule the sealer enforces (re-run through sealFoldRecord), and
byte equality with the canonical re-encoding. Any failure is
InvalidResponse naming the key and the check.
Tree GC keeps its progress at <P>/_v99/gc-pending.json: the head
generation and floor it is bound to, a write counter, the last probe's
server time and the local time of that probe, the resolved floors, a
stuck floor, one work entry per resolved floor and the leak sweep's
cursor and marks. The state schedules and never authorizes a deletion,
so a stale or lost one can only leak.

The encoder refuses a state the decoder would refuse. The decoder
returns a malformed state as a value, since GC replaces it leak-only,
and throws InvalidResponse only for this format at another schema
version, saying GC for the collection is paused.

Constants: the state format tag, schema version and body ceiling, the
larger per-tick candidate ceiling a roomy ledger allows (512), and the
calls a tick reserves besides its candidate checks.
runTreeGcTick reclaims tree nodes no current root reaches, and never
writes the head. A record's floor is closed once the head's floor is
above it: that attempt's head CAS can no longer land. The winner of a
closed floor is named by evidence its CAS created (the head's
installer, or the base installer of a record captured on a later
floor); every other attempt there is a loser.

- Losers' fresh nodes are deleted at once.
- Winners' superseded nodes are deleted 15 minutes of server time
  after a probe first stamps them, so reads through the old root can
  finish; a read that outlives that meets the reader's backstop.
- A leak sweep lists the node prefix a page per probe, notes
  unreachable nodes with the head floor read after the LIST, stamps
  them once a later fold moved the floor, and deletes them 60 minutes
  later.

Every DELETE is proven in its own tick by the membership check against
the root that tick read (key-only at depth 0 and 1; a body GET and a
validated path above). A missing or undecodable path node stops the
tick's deletions. Records, the state and the clock only nominate and
pace. The only clock is the server time of GC's own state write,
probed at most once a minute of local time judged against the local
time of the last probe; no server time means nothing is due. A corrupt
candidate is counted and warned and left in place, except a known
loser's, which is deleted on its key alone. An identity found in two
records is never a deletion source. Candidate checks per tick are
sized from the request ledger: 512 when it has room, else at most 64.

Tests run real folds against a harness with hand-moved local and
server clocks and folds paused before their head CAS, judge the tree
with the physical oracle and a reader-margin watch, sweep a crash
across a tick, and turn 22 GC mutants red, including record path,
membership, sweep, classification, clock and identity defects.
Add the tree GC tick's static bound, 43 + K x c(d) plus log
retirement, with c(d) = 3 at depth 0 and 1 and 3(d + 1) above, and
the Class A writes tree GC adds per minute (a record create and at
most one resolving state write per fold, at most two state writes per
probe minute). Tests pin the bound at 64 checks for depths 0 to 4,
4,720 calls at 512 checks on depth 2, and agreement with the tick's
own sizing.
- An identity that appears in two records, or has a record on an open
  floor, is never a deletion source: a loser entry naming one is
  cleared before it drains, every record of a duplicated identity is
  retired leak-only (a named loser no longer exempts it), a winner of
  one is quarantined, and the sweep neither notes, stamps nor deletes
  its nodes. Before, a duplicate found after a loser entry was
  assigned still drained it, and a stamped mark could be deleted after
  an attempt reusing its identity adopted the key.
- The sweep page's notes count toward the tick's candidate checks: a
  short ledger shortens the page, and the cursor moves past only the
  keys examined. Before, a probe tick with room for one check made
  sixteen.
- A state naming another format, or this format at any schema version
  but 1, pauses GC with InvalidResponse instead of being replaced, so
  no build wipes a state it cannot read.
- The membership check answers "not-a-node", never "non-member", for a
  key that does not parse.

Tests cover each schedule (red against the previous tick), plus record
generations as evidence, a late supersession of a dropped mark and
winner quarantine when the record DELETE cap runs out. The mutation
block now rejects a red that is only an unexplained throw, and the
open-floor check asserts the leak it causes. Sweep scenarios hide the
in-flight record from LIST, so they test the floor gate rather than
the open-floor guard.
The record LIST runs every tick and the sweep LIST every probe, and
both are Class A. With in-band ticks the record LIST grows with the
write rate, so the rate now takes ticks per minute as well as folds.
Every in-band tick used to LIST the fold records, so GC's Class A cost
grew with the write rate (about one LIST per write). Now only a probe
tick, at most one per TREE_GC_CLOCK_PROBE_MINUTES of local time, runs
the collecting body: sweep and record LISTs, head and root reads,
resolution, deletes and state writes. Any other tick GETs the state,
retires the log and ends with the new `paced` outcome.

Skipping only the record LIST was not enough: that LIST builds the
duplicate and open-floor identity sets every delete is checked
against, so a tick that deleted from state without it would delete
unchecked. With the gate, every delete follows a record LIST and a
won probe CAS in the same tick, and due tests always use that probe's
server time. Reclaim waits at most one more probe interval, well
inside the 15-minute superseded delay.

The op model's Class A rate drops its per-tick term: one record per
fold plus four calls per probe.
Resolving closed floors retired stale, stuck, untrusted and unusable
fold records before the probe write, so a tick whose probe CAS lost
returned `conflict` having already deleted. The resolution step now
queues those retirements under the same per-tick record-delete cap
and the tick issues them only once its probe CAS has won; a lost
probe deletes nothing.

Adds a `gcRetiresBeforeProbe` step seam and the
`gc-deletes-before-probe` mutant, caught by a new lost-probe test.
A work entry whose winner identity showed up in another record stays
quarantined, and the winner drain skipped it without ever retiring
its record. When the per-tick record-delete cap retired the other
records under that identity first, the winner's record was left as
the only one, the slot never dropped, and enough such slots fill
TREE_GC_MAX_WORK_ENTRIES and stop GC.

A quarantined slot now retires its winner record leak-only, deleting
no node it names, once its floor's loser is done, and drops.
The record drains treated a `deferred` membership result (a body
that failed its decode once and read cleanly on the re-read) as done,
so the slot's cursor moved past the node and, once the list ended,
its record was retired: the node leaked until the sweep found it.

A deferred node now stops that slot's advance for the tick, so the
next probe checks it again; other slots go on draining.
The record page counted only keys that parse as fold-record keys when
deciding whether it was full, but keys under the record prefix that do
not parse still use up the LIST's maxKeys. A full page with stray keys
read as complete, so a winner slot whose record lay past the page was
dropped as gone and its record leaked, and a winner could be retired
while a loser at its floor sat unlisted.

The page is now full when the listed-entry count reaches maxKeys,
whatever those keys are.
A `budget` outcome returned after the sweep LIST and the head and
root GETs, before the probe write and without retiring the log, so
every following tick on a short ledger paid those calls again and
log retirement never ran.

A probe tick whose ledger has no room for one check even at depth 0
now skips straight to log retirement after the state GET, as a paced
tick does, listing nothing. One whose ledger only the root's depth
rules out writes the probe alone, so the ticks of the next probe
interval are paced, and then retires the log. Both still count
`db.maintenance.request_budget_skipped_total`.
A tick probed whenever its local time read earlier than the previous
probe's. Writers on different instances share one GC state, so two
whose clocks differ by a minute or more each saw the other's probe in
their future or past and probed on every tick.

A tick now probes when there is no previous probe, when its local time
is at least TREE_GC_CLOCK_PROBE_MINUTES past the previous one, or when
it reads more than the new TREE_GC_CLOCK_RESET_MINUTES (60) before it.
A smaller backward reading is skew between instances and waits for
the probe interval. Local time is still only compared with local time
and never decides a deletion.
The winner drain checked that the winner's identity was trusted but
not the identity each superseded node carries, unlike the sweep. A
winner can supersede every node of an earlier attempt; if an attempt
reusing that identity is then in flight and adopts those nodes, the
drain deleted them as non-members of the current root and the
reusing attempt landed on a tree with missing nodes.

A superseded ref whose own attempt is a duplicate or still publishing
is now skipped, leak-only, and left to the sweep.
@baer
baer added this pull request to stack #440 November 25, 2026 20:21
@baer baer changed the title tree/gc tick feat(tree): add the tree GC tick Nov 25, 2026
Keep duplicate-identity records while their floor can still publish so
later GC ticks retain the guard for adopted nodes. Add a regression
that verifies a paused adopter survives a due sweep with a duplicate
record.

Validation: regression fails before the fix; 113 focused tests pass.
Repository verification and all 14 bundle budgets pass.
@baer
baer merged commit 2f3016b into main Nov 25, 2026
7 checks passed
@baer
baer deleted the tree/gc-tick branch November 25, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant